package graphicView;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;

import model.*;

/**
 * Team EBE
 * 
 * PackageGUI.java is the user interface to edit a package
 * 
 */

public class PackageGUI extends JPanel implements ActionListener, Observer,
		ListSelectionListener {
	private static final long serialVersionUID = 1L;
	protected JFrame packageframe;
	private GraphicController controller;

	// declare the components
	JTextField packageNameField = new JTextField(30);
	JTextField heightField = new JTextField(5);
	JTextField widthField = new JTextField(5);
	JButton updatePackage = new JButton("Update Package");
	JButton cancelChanges = new JButton("Cancel Changes");
	JButton finishChanges = new JButton("Finish Changes");

	// the package we will be editing
	PackageEditor theEditor;

	public PackageGUI(GraphicController Controller, JFrame cFrame,
			PackageEditor editor) {
		packageframe = cFrame;
		controller = Controller;
		theEditor = editor;

		initFields();
		JPanel packagePanel = setupPackageLayout();
		packageframe.getContentPane().add(packagePanel, BoxLayout.X_AXIS);
	}

	private void initFields() {
		packageNameField.setText(theEditor.getName());
		String h = Integer.toString(theEditor.getySizeY());
		heightField.setText(h);
		String w = Integer.toString(theEditor.getSizeX());
		widthField.setText(w);
		
	}

	private JPanel setupPackageLayout() {

		// create the mane panel for the inspector uses box layout to stack
		// elements vertically
		JPanel packagePanel = new JPanel();
		packagePanel.setLayout(new BoxLayout(packagePanel, BoxLayout.Y_AXIS));

		// sets the command to issue to the listener when the buttons are
		// pressed

		updatePackage.setActionCommand("package");

		cancelChanges.setActionCommand("cancel");

		finishChanges.setActionCommand("finish");

		updatePackage.addActionListener(this);
		cancelChanges.addActionListener(this);
		finishChanges.addActionListener(this);

		// creates and adds the panel for class name		
		
		JPanel packageNamePanel = new JPanel();
		packageNamePanel.setLayout(new GridLayout(3,2,2,2));
		packageNamePanel.add(new JLabel("Package name"));
		packageNamePanel.add(packageNameField);
		packageNamePanel.add(new JLabel("Height"));
		packageNamePanel.add(heightField);
		packageNamePanel.add(new JLabel("Width"));
		packageNamePanel.add(widthField);

		packagePanel.add(packageNamePanel);

		// creates a panel for the update and cancel and finish buttons
		JPanel packageButtonPanel = new JPanel();
		packageButtonPanel.setLayout(new GridLayout(3, 1));
		packageButtonPanel.add(updatePackage);
		packageButtonPanel.add(cancelChanges);
		packageButtonPanel.add(finishChanges);

		packagePanel.add(packageButtonPanel);

		packagePanel.setPreferredSize(new Dimension(250, 200));

		return packagePanel;

	}

	public void actionPerformed(ActionEvent e) {
		if ("cancel".equals(e.getActionCommand())) {
			initFields();
		} else if ("package".equals(e.getActionCommand())) {
			int heightInt = Integer.parseInt(heightField.getText());
			int widthInt = Integer.parseInt(widthField.getText());
			
			
			theEditor.commitEdits(packageNameField.getText(), heightInt, widthInt);
			repaint();
		} else if ("finish".equals(e.getActionCommand())) {

			// classframe.dispose();
			packageframe.setVisible(false);
		}
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		initFields();
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		// if (e.getValueIsAdjusting() == false) {
		//
		// if (attributeList.getSelectedIndex() == -1) {
		// deleteAttribute.setEnabled(false);
		//
		// if(operationList.getSelectedIndex() == -1){
		// deleteOperation.setEnabled(false);
		// }
		// else{
		// deleteAttribute.setEnabled(true);
		// deleteOperation.setEnabled(true);
		// }
		// }
	}
	

}
